package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.dao.log.ReturnPosLogDao;
import com.tbit.main.pojo.Point;
import com.tbit.main.pojo.ReturnPosLog;
import com.tbit.main.service.DataService;
import com.tbit.main.service.ReturnPosLogService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.GeoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2023-05-06 15:43
 */
@Service
public class ReturnPosLogServiceImpl implements ReturnPosLogService {

    @Autowired
    private CalibrateServiceImpl calibrateService;
    @Autowired
    private ReturnPosLogDao returnPosLogDao;
    @Autowired
    private DataService dataService;

    @Override
    public List<ReturnPosLog> getPos(Double lon, Double lat, Integer distance, Integer day, Integer mapType, Integer posFrom, Integer parkPointId, Integer inside) {

        if (day == null) {
            day = 7;
        }
        if (distance == null) {
            distance = 100;
        }

        Map<String, Object> map = new HashMap<>();
        Double area = 0.00001D * distance;
        map.put("lonMax", lon + area);
        map.put("lonMin", lon - area);
        map.put("latMax", lat + area);
        map.put("latMin", lat - area);
        map.put("posTime", DateTimeUtil.getDayAgoTime(day));
        map.put("posFrom", posFrom);
        List<ReturnPosLog> returnPosLogs = returnPosLogDao.getPos(map);

        /**筛选定位点*/
        if (parkPointId != null) {
            List<Point> points = dataService.getParkList(parkPointId);
            /**站内*/
            if (inside == 1) {
                returnPosLogs = returnPosLogs.parallelStream().filter(e -> GeoUtil.IsPtInPoly(new Point(e.getLon(), e.getLat()), points)).collect(Collectors.toList());
            }
            /**站外*/
            else if (inside == 0) {
                returnPosLogs = returnPosLogs.parallelStream().filter(e -> !GeoUtil.IsPtInPoly(new Point(e.getLon(), e.getLat()), points)).collect(Collectors.toList());
            }
        }

        /**校准经纬度*/
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }
        calibrateService.calibrateOffSetReturn(returnPosLogs, mapType);

        return returnPosLogs;
    }

    @Override
    public List<ReturnPosLog> getReturnPosLog(Integer mapType, List<String> orderNos) {
        if (CollUtil.isEmpty(orderNos)) {
            return Collections.emptyList();
        }

        /**校准经纬度*/
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        List<ReturnPosLog> returnPosLogs = returnPosLogDao.selectReturnPosLog(orderNos);
        calibrateService.calibrateOffSetReturn(returnPosLogs, mapType);
        return returnPosLogs;
    }
}
